package com.sanyang.pda.presenter;


import com.google.gson.Gson;
import com.sanyang.pda.bean.HaveTestedBean;
import com.sanyang.pda.bean.PalletBean;
import com.sanyang.pda.bean.request.SampleReturnRequest;
import com.sanyang.pda.contract.SampleBackContract;
import com.sanyang.pda.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class SampleBackPresenter extends BasePresenter<SampleBackContract.View> implements SampleBackContract.Presenter {

    @Override
    public void sampleReturn(List<SampleReturnRequest> sampleReturnRequest) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(sampleReturnRequest));
        NetUtils.geApi().sampleReturn(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<PalletBean>>() {
                    @Override
                    public void accept(BaseAppEntity<PalletBean> palletBeanBaseAppEntity) throws Exception {
                        getView().onSuccess();
                        getView().showMessage(palletBeanBaseAppEntity.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void samplingResultList(String en) {
        NetUtils.geApi().samplingResultList(en)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<HaveTestedBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<HaveTestedBean>> listBaseRowsAppEntity) throws Exception {
                        getView().setList(listBaseRowsAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void sampleIsNotBack(List<String> strings) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(strings));
        NetUtils.geApi().sampleIsNotBack(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<PalletBean>>() {
                    @Override
                    public void accept(BaseAppEntity<PalletBean> palletBeanBaseAppEntity) throws Exception {
                        getView().onSuccess();
                        getView().showMessage(palletBeanBaseAppEntity.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
